exports.FindOneData = function({model,query,data,callback,res,msg="成功！"}){
    model.findOne(
        query,
        data
    ).then((result)=>{
        if(callback){
            callback(result);
        } else {
            res.json({
                code:200,
                msg:msg,
                data:result
            });
        }
    }).catch((err)=>{
        res.json({
            code:500,
            data:err,
            msg:"服务器错误"
        })
    })
}

exports.FindManyData = function({model,query,callback,res,msg,sort,limit=0,skip=0}){
    model.find(query)
    .sort(sort)
    .limit(limit)
    .skip(skip)
    .then(result=>{
        if( callback ){
            callback(result);
        } else {
            res.json({
                code:200,
                msg:msg || '查询成功！',
                data:result
            });
        }
    }).catch(err=>{
        res.json({
            code:500,
            data:err,
            msg:'服务器异常！'
        });
    })
}

exports.InsertManyData = function ({model,data,callback,res,msg}){
    model.insertMany(data).then(result=>{
        if(callback){
            callback(result);
        } else {
            res.json({
                code:200,
                msg: msg || '数据插入成功！' ,
                data:result
            });
        }
    }).catch(err=>{
        res.json({
            code:500,
            msg:'服务器异常',
            data:err
        });
    })
}

exports.UpdateManyData = function ({model,query,data,callback,res,msg}){
    model.updateMany(
        query,
        data
    ).then(result=>{
        if(callback){
            callback(result);
        } else {
            res.json({
                code:200,
                msg:msg || '修改成功！',
                data:result
            });
        }
    }).catch(err=>{
        res.json({
            code:500,
            msg:'服务器异常',
            data:err
        });
    });
}

exports.RemoveData = function ({model,query,callback,res,msg}){
    model.remove(
        query
    ).then(result=>{
        if(callback){
            callback(result);
        } else {
            res.json({
                code:200,
                msg:msg || '删除成功！',
                data:result
            });
        }
    }).catch(err=>{
        res.json({
            code:500,
            msg:'服务器异常',
            data:err
        });
    });
}